export default {
  namespaced: true,
  state() {
    const newAddress = uni.getStorageSync('101-address')
    const userinfo = uni.getStorageSync('101-userinfo')
    return {
      token: '123',
      address: newAddress ? JSON.parse(newAddress) : {},
      userinfo: userinfo ? JSON.parse(userinfo) : {},
    }
  },
  mutations: {
    setAddress(state, payload) {
      state.address = payload
      uni.setStorageSync('101-address', JSON.stringify(state.address))
    },
    // 个人信息持久化存储
    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo

      // 持久化到本地缓存
      uni.setStorageSync('101-userinfo', JSON.stringify(state.userinfo))
    },
  },
  actions: {},
  getters: {
    fullAddress(state) {
      const { provinceName, cityName, countyName, detailInfo } = state.address
      if (!provinceName) return ''
      // 拼接 省，市，区，详细地址 的字符串并返回给用户
      return provinceName + cityName + countyName + detailInfo
    },
  },
}
